<%@include file="_header.jspf" %>
<%@page import="com.spartancoder.accommodation.models.University" %>
<%@page import="com.spartancoder.accommodation.beans.UniversityBean" %>
<%@page import="com.spartancoder.accommodation.beans.CollegeBean" %>
<%@page import="com.spartancoder.accommodation.models.College" %>
<%

String error = null;

String name = request.getParameter("name");
String address = request.getParameter("address");

String id = request.getParameter("universityId");
int universityId = 0;

if( name != null && address != null && id != null ) {
    universityId = Integer.parseInt(id);
    if( universityId == 0 ) {
        error = "Selecteaza o universitate";
    } else if( name.trim().isEmpty() ) {
        error = "Completeaza un nume";
    } else {
        CollegeBean cb = new CollegeBean();
        cb.setUniversityId(universityId);
        cb.setName(name);
        cb.setAddress(address);
        cb.setAvailable(true);
        College.create(cb);
        session.setAttribute("flash", "Facultatea a fost adaugata");
        response.sendRedirect("list-college.jsp?id=" +universityId);
    }
} else {
    name = "";
    address = "";
}


%>

<h2>Adauga facultate</h2>

<form action="add-college.jsp" method="post" class="span-12">
        <fieldset>
        <% if(error !=null) { %>
        <br /><div class="error"><%=error%></div>
        <% } %>

        <label class="text large">Universitate:</label><br />
        <select name="universityId" class="text large">
            <option value="0">--- selecteaza universitate ---</option>
            <% for( UniversityBean ub : University.fetchAll() ) { %>
            <option <% if(universityId == ub.getUniversityId()) { %>selected="selected"<%}%>
                value="<%=ub.getUniversityId()%>"><%=ub.getName()%></option>
            <% } %>
        </select><br />

        <label class="text large">Nume:</label><br />
        <input type="text" name="name" value="<%=name%>" class="text large" /><br />

        <label class="text large">Adresa</label><br />
        <textarea name="address" class="text large" style="height: 50px;"><%=address%></textarea>

        <button type="submit" class="button positive">
            Adauga facultate
        </button>

    </fieldset>
</form>

<%@include file="_footer.jspf" %>